package com.antoco.controlcenter.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.antoco.controlcenter.R;
import com.antoco.controlcenter.entity.RebotEntity;
import com.antoco.controlcenter.iface.IBatteryListener;

import java.text.NumberFormat;
import java.util.List;


public class ShowListRecyclerViewAdapter extends RecyclerView.Adapter<ShowListRecyclerViewAdapter.ViewHolder> {
    private List<RebotEntity> mlist;
    private Context context;
    private OnItemClickListener mOnItemClickListener;
    private IBatteryListener iBatteryListener;

    public static interface OnItemClickListener {
        void onItemClick(int position);

        void onItemDelete(int position);

        void onItemLongClick(int position);
    }

    public void setIBatteryListener(IBatteryListener iBatteryListener) {
        this.iBatteryListener = iBatteryListener;
    }

    public ShowListRecyclerViewAdapter(Context context, List<RebotEntity> list, OnItemClickListener onItemClickListener) {
        super();
        mlist = list;
        this.context = context;
        mOnItemClickListener = onItemClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        // each data item is just a string in this case
        public TextView title;
        public TextView content;

        public ViewHolder(View itemView) {
            super(itemView);

        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
//                case R.id.ll:
//                    mOnItemClickListener.onItemClick(getAdapterPosition());
//                    break;
//                case R.id.delete:
//                    mOnItemClickListener.onItemDelete(getAdapterPosition());
//                    break;
//                case R.id.recovery:
//                    mOnItemClickListener.onItemDownload(getAdapterPosition());
//                    break;
            }
        }

        @Override
        public boolean onLongClick(View v) {
            switch (v.getId()) {
//                case R.id.ll:
//                    mOnItemClickListener.onItemLongClick(getAdapterPosition());
//                    break;
            }
            return false;
        }
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ShowListRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                                     int viewType) {
        // create a new view
        View convertView = LayoutInflater.from(context)
                .inflate(R.layout.show_list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(convertView);
        viewHolder.title = convertView.findViewById(R.id.title);
        viewHolder.content = convertView.findViewById(R.id.content);

        return viewHolder;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        String str = mlist.get(position).getValue();
        int color = (0 == mlist.get(position).getColor()) ? Color.BLUE : mlist.get(position).getColor();
        if (TextUtils.isEmpty(str)) {
            return;
        }
        String[] strings = str.split("：");
        if (strings.length >= 1) {
            holder.title.setText(strings[0]);
            holder.title.setTextColor(color);
        }

        if (strings.length >= 2) {

            if (holder.title.getText().toString().contains("电量")) {
                String dlstr = strings[1].replace("V", "").replace("%.", "");
                //35.1~55
                float dl = Float.parseFloat(dlstr);
                float abs = Math.abs(dl - 35.1f);
                if (abs > 20) {
                    abs = 0;
                }
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(0);
                String result = numberFormat.format((abs / 20) * 100);
                if (TextUtils.isEmpty(result)) {
                    result = "0";
                }
                if (null != iBatteryListener) {
                    iBatteryListener.onDeviceBattery(Integer.parseInt(result));
                }
            }
            holder.content.setText(strings[1]);
            holder.content.setTextColor(color);
        }

    }


    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mlist.size();
    }

    public void refreshData(List<RebotEntity> list) {
        if (null != mlist) {
            mlist.clear();
        }
        mlist.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        if (null != mlist) {
            mlist.clear();
        }
        notifyDataSetChanged();
    }

    // 添加数据
    public void addData(RebotEntity map, int position) {
        mlist.add(map);
        //添加动画
        notifyItemInserted(position);
    }

    public void deleteItem(int position) {
        mlist.remove(position);
        notifyItemRemoved(position);
    }


    public List<RebotEntity> getAllList() {
        return mlist;
    }

}
